package server.networking;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * The Server class serves as a starting point for the Royally Flushed server.
 * It fetches and sets the security policies based on the server.policy file,
 * then broadcasts the ServerRemote stub using RMI.
 */

public class Server {
	public static void main(String[] args) {
		final int port = 1250;
		
		System.out.println("--- RMI Server ---");
		System.out.println("Starting RMI Server...");
		
		// Establish access policies
		System.setProperty("java.security.policy", PolicyGetter.getPolicyLocation("server"));
		System.setProperty("java.rmi.server.hostname", "0.0.0.0");
		
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		
		try {
			
			ServerRemote server = new ServerRemote();
			ServerRemoteInterface serverStub = (ServerRemoteInterface) UnicastRemoteObject.exportObject(server, port);
			
			Registry reg = LocateRegistry.getRegistry();
			reg.rebind("Server", serverStub);
		
			System.out.println("RMI Server started! Accepting requests.");
		}
		catch(Exception e) {
			System.out.println("An error occurred.");
			e.printStackTrace();
		}
	}
}
